<template>
  <span>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{ scope.$index + page.pageIndex*page.pageSize + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名">
        <template slot-scope="{row}">
          <UserItem :userid="row.userName" />
        </template>
      </el-table-column>
      <el-table-column prop="duty" label="党内分工">
        <PartyDuty slot-scope="{row}" :type="row.duty" />
      </el-table-column>
      <el-table-column label="起任时间">
        <template slot-scope="{row}">
          <div>{{ row.duty.length === 1 && row.duty[0]===0 ?'无' :parseTime(row.dutyStart,'{y}年{m}月{d}日') }}</div>
        </template>
      </el-table-column>
      <el-table-column label="入党时间">
        <template slot-scope="{row}">
          <div>{{ parseTime(row.time_Party,'{y}年{m}月{d}日') }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" class="red--text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination :pagesetting.sync="page" :total-count="totalCount" />
  </span>
</template>
<script>
import { updatePartyUser } from '@/api/zzxt/party-userinfo'
import { parseTime } from '@/utils'
export default {
  name: 'Table',
  components: {
    Pagination: () => import('@/components/Pagination'),
    UserItem: () => import('@/components/User/UserFormItem'),
    PartyDuty: () => import('@/components/Party/PartyDuty')
  },
  props: {
    list: { type: Array, default: () => [] },
    data: { type: Object, default: null },
    totalCount: { type: Number, default: 0 }
  },
  data: () => ({
    page: { pageIndex: 0, pageSize: 10 },
    loading: false
  }),
  computed: {},
  watch: {
    page: {
      handler(val) {
        this.$emit('update:page', val)
      },
      deep: true
    }
  },
  methods: {
    parseTime,
    handleEdit(row) {
      this.$emit('update:data', row)
      this.$nextTick(() => {
        this.$emit('requireEdit')
      })
    },
    async handleDelete(row) {
      await this.$confirm(`确定要删除 ${row.userRealName} 的信息吗`)
      const i = Object.assign(row, { isRemoved: true })
      this.$emit('update:loading', true)
      updatePartyUser(i)
        .then(data => {
          this.$message.success('成功删除')
          this.$emit('requireRefresh')
        })
        .finally(() => {
          this.$emit('update:loading', false)
        })
    }
  }
}
</script>
